package yxy.game.pm2.module.capturestrategicpoints;

import org.joda.time.DateTime;
import yxy.apple.util.ActiveUtil;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.clearancemonster;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wrs
 * @descrition 斩关夺隘Module
 * @since 2022-08-15
 */
public class CaptureStrategicPointsModule extends PlayerModule {

    // bean===========================================================================

    private int degree; //增强的等级，初始为零
    private DateTime endTime; //结束时间，时长为一周
    private DateTime updateTime; //上一次更新时间
    private HashMap<Integer, HashMap<Integer, Boolean>> openCustomsMap = new HashMap<>(); // key1：对应的天数  key2：关卡数  value：当前的关卡是否通关
    private HashMap<Integer, Integer> checkpointRecord = new HashMap<>(); //key  天数   value  通关数量
    private HashMap<Integer, ArrayList<Integer>> checkpointRewardRecord = new HashMap<>(); //key  天数   value  领取过的奖励
    private ArrayList<Integer> boxMap = new ArrayList<>(); //过关达标奖励
    private ArrayList<Integer> starMap = new ArrayList<>(); //key  怪物表id(用来统计星星数量)
    private int totalStars; //总星数
    private int starsLevel; //观星等级
    private int day; //当天是一周中的第几天
    private ArrayList<Format> formats = new ArrayList<>();// 阵容

    public ArrayList<Integer> getStarMap() {
        return starMap;
    }

    public void setStarMap(ArrayList<Integer> starMap) {
        this.starMap = starMap;
    }

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }

    public DateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(DateTime endTime) {
        this.endTime = endTime;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    public HashMap<Integer, HashMap<Integer, Boolean>> getOpenCustomsMap() {
        return openCustomsMap;
    }

    public void setOpenCustomsMap(HashMap<Integer, HashMap<Integer, Boolean>> openCustomsMap) {
        this.openCustomsMap = openCustomsMap;
    }

    public ArrayList<Integer> getBoxMap() {
        return boxMap;
    }

    public void setBoxMap(ArrayList<Integer> boxMap) {
        this.boxMap = boxMap;
    }

    public int getTotalStars() {
        return totalStars;
    }

    public void setTotalStars(int totalStars) {
        this.totalStars = totalStars;
    }

    public int getStarsLevel() {
        return starsLevel;
    }

    public void setStarsLevel(int starsLevel) {
        this.starsLevel = starsLevel;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public ArrayList<Format> getFormats() {
        return formats;
    }

    public void setFormats(ArrayList<Format> formats) {
        this.formats = formats;
    }

    public HashMap<Integer, Integer> getCheckpointRecord() {
        return checkpointRecord;
    }

    public void setCheckpointRecord(HashMap<Integer, Integer> checkpointRecord) {
        this.checkpointRecord = checkpointRecord;
    }

    public HashMap<Integer, ArrayList<Integer>> getCheckpointRewardRecord() {
        return checkpointRewardRecord;
    }

    public void setCheckpointRewardRecord(HashMap<Integer, ArrayList<Integer>> checkpointRewardRecord) {
        this.checkpointRewardRecord = checkpointRewardRecord;
    }

    // func===========================================================================

    private static CaptureStrategicPointsModule create(String playerCode) {
        CaptureStrategicPointsModule module = new CaptureStrategicPointsModule();
        module.setDegree(0);//难度等级为0
        ArrayList<Format> formats = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Format format = new Format();
            formats.add(format);
        }
        module.setFormats(formats);
        module.setTotalStars(0);
        module.setStarsLevel(0);
        module.setPlayerCode(playerCode);
        reset(module);
        module.set();
        return module;
    }

    // redis==========================================================================

    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:CaptureStrategicPoints", playerCode);
    }


    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public CaptureStrategicPointsModule get(String playerCode) {
        try {
            CaptureStrategicPointsModule module = getv(key(playerCode));

            //todo 后期放数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), CaptureStrategicPointsModule.class);
                if (module != null) {
                    module.set();
                }//把数据同步到redis
            }

            if (module == null) {
                module = create(playerCode);
            }
            module.update(module);
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private void update(CaptureStrategicPointsModule module) {
        DateTime now = DateTime.now();

        //在结束时间后面再登录，重置整个玩法
        DateTime endTime = module.getEndTime();
        if (endTime.isBefore(now)) {
            //刷新难度
            resetDegree(module);
            //重置
            reset(module);
            module.set();
            return;
        }

        //判断第几天
        int days = ActiveUtil.days(getUpdateTime(), now);
        if (days >= 1) {        //转天更新
            int activityHasPassTime = getDay() >= 6 ? 6 : ActiveUtil.days(getUpdateTime(), now) + 1;
            setDay(activityHasPassTime);
            setUpdateTime(now);
            set();
        }

    }

    //更新难度
    private void resetDegree(CaptureStrategicPointsModule module) {
        int degree = module.getDegree();
        Boolean isAddDegree = true;

        HashMap<Integer, Integer> checkpointRecord = module.getCheckpointRecord();

        if (checkpointRecord.size() < 6) {
            isAddDegree = false;
        }

        for (Integer value : checkpointRecord.values()) {
            if (value < 3) {
                isAddDegree = false;
            }
        }

        //如果都打过了,增加难度，否则降低难度，最低难度为0
        degree = isAddDegree ? degree + 1 : degree - 1 >= 0 ? degree - 1 : 0;
        module.setDegree(degree);
    }

    //重置
    private static void reset(CaptureStrategicPointsModule module) {
        DateTime now = DateTime.now();
        module.setUpdateTime(now);
        module.setEndTime(ActiveUtil.time23_30(now.plusDays(6)));

        Map<String, clearancemonster> allData = DataCenter.getAllData(clearancemonster.class);
        HashMap<Integer, HashMap<Integer, Boolean>> map = new HashMap<>();

        for (clearancemonster value : allData.values()) {
            //不包含初始化
            if (!map.containsKey(value.getField())) {
                HashMap<Integer, Boolean> hashMap = new HashMap<>();
                hashMap.put(value.getId(), false);
                map.put(value.getField(), hashMap);
                continue;
            }

            //包含，往里面添加数据(怪物id和reward表的id是一样的)
            if (map.containsKey(value.getField())) {
                HashMap<Integer, Boolean> hashMap = map.get(value.getField());
                hashMap.put(value.getId(), false);
            }
        }

        module.setOpenCustomsMap(map);//6天的怪物
        module.setBoxMap(new ArrayList<Integer>());
        module.setCheckpointRecord(new HashMap<>());
        module.setCheckpointRewardRecord(new HashMap<>());
        module.setStarMap(new ArrayList<>());
        module.setDay(1);
    }


    //红点
    private void check() {

    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                CaptureStrategicPointsModule module = getv(key(playerCode));
                if (module != null) MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }


    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
